package com.yanglei.server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.DefaultButtonModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.yanglei.util.CharacterUtil;
import com.yanglei.util.XMLUtil;

public class Server extends JFrame {

	private JLabel jlabel1;
	private JLabel jlabel2;
	private JLabel jlabel3;
	private JButton jbutton;
	private JButton jbutton2;
	private JPanel jpanel1;
	private JPanel jpanel2;
	private JScrollPane jscrollpane1;
	private JTextArea jtextarea1;
	private JTextField jtextfield1;
	private ServerMessageThread smt;
	private Map<String, ServerMessageThread> map = new HashMap<String, ServerMessageThread>();

	private final DefaultListModel dlm = new DefaultListModel();

	private JList jlist;

	private ServerConnection serverConnection;

	private String username;

	public JTextArea getJtextarea1() {
		return jtextarea1;
	}

	public DefaultListModel getDlm() {
		return dlm;
	}

	public JLabel getJlabel2() {
		return jlabel2;
	}

	public Map<String, ServerMessageThread> getMap() {
		return map;
	}

	public JButton getJbutton() {
		return jbutton;
	}

	public Server(String name) {

		super(name);
		this.initComponets();
	}

	private void initComponets() {
		jpanel1 = new JPanel();
		jpanel2 = new JPanel();

		jlabel1 = new JLabel();
		jlabel2 = new JLabel();
		jlabel3 = new JLabel();

		jtextfield1 = new JTextField(10);
		jbutton = new JButton();
		jbutton2 = new JButton("踢出 ");
		jscrollpane1 = new JScrollPane();
		jtextarea1 = new JTextArea();

		jtextfield1.setText("9988");

		jpanel1.setBorder(BorderFactory.createTitledBorder("服务器信息"));
		jpanel2.setBorder(BorderFactory.createTitledBorder("用户列表"));

		jlabel1.setText("服务器状态 ");
		jlabel2.setText("停止");
		jlabel2.setForeground(new Color(204, 0, 51));
		jlabel3.setText("端口号");

		jbutton.setText("启动服务器");

		jbutton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				execute();
			}
		});

		this.addWindowListener(new WindowAdapter() {
			// 关闭服务器
			public void windowClosing(WindowEvent e) {

				try {
					Collection<ServerMessageThread> cols = Server.this.map
							.values();

					String messageXML = XMLUtil.construcCloseServerWindowXML();

					for (ServerMessageThread smt : cols) {
						smt.sendMessage(messageXML);
					}
				} catch (Exception e2) {
					// TODO: handle exception
				} finally {
					System.exit(0);
				}
			}

		});

		this.jtextarea1.setEditable(false);
		this.jtextarea1.setForeground(new Color(245, 0, 0));

		jpanel1.add(jlabel1);
		jpanel1.add(jlabel2);
		jpanel1.add(jlabel3);
		jpanel1.add(jtextfield1);
		jpanel1.add(jbutton);

		jtextarea1.setColumns(30);
		jtextarea1.setForeground(new Color(0, 51, 204));
		jtextarea1.setRows(20);

		jlist = new JList(dlm);
		jlist.setVisibleRowCount(20);
		jscrollpane1.setViewportView(jlist);

		// 鼠标点击两次 选中该用户
		jlist.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				if (jlist.getSelectedIndex() != -1) {
					if (e.getClickCount() == 2) {
						username = (String) jlist.getSelectedValue();
					}
				}
			}

		});
		// 单击踢出 弹出提示信息
		jbutton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				twoClick(username);

			}
		});

		jpanel2.add(jscrollpane1, BorderLayout.WEST);
		jpanel2.add(jbutton2, BorderLayout.EAST);

		this.getContentPane().add(jpanel1, BorderLayout.NORTH);
		this.getContentPane().add(jpanel2, BorderLayout.SOUTH);

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.pack();
		this.setVisible(true);
	}

	private void execute() {
		String hostPort = this.jtextfield1.getText();

		if (CharacterUtil.isEmpty(hostPort)) {
			JOptionPane.showMessageDialog(this, "端口号不能为空", "警告",
					JOptionPane.WARNING_MESSAGE);

			this.jtextfield1.requestFocus();

			return;
		}

		if (!CharacterUtil.isNumber(hostPort)) {
			JOptionPane.showMessageDialog(this, "端口号必须为数字", "警告",
					JOptionPane.WARNING_MESSAGE);

			this.jtextfield1.requestFocus();

			return;
		}

		if (!CharacterUtil.isPortCorrect(hostPort)) {
			JOptionPane.showMessageDialog(this, "端口号必须在1024-65535之间", "警告",
					JOptionPane.WARNING_MESSAGE);

			this.jtextfield1.requestFocus();

			return;
		}

		int port = Integer.parseInt(this.jtextfield1.getText());

		this.serverConnection = new ServerConnection(this, port);
		this.serverConnection.start();

	}

	public static void main(String[] args) {
		new Server("服务器");
	}

	private void twoClick(String username) {
		int choose = JOptionPane.showConfirmDialog(this, "是否删除该用户", "警告",
				JOptionPane.WARNING_MESSAGE);

		if (choose == 0) {
			this.serverConnection.getServerMessageThread().removeUser(username,
					"12");
		}
	}
}
